﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Linq;
using System.Text;
using DomainDriver.CommonUtilities.Enums;
using DomainDriver.DomainModeling.DomainObjects;
using DomainDriver.DomainModeling.StorageManagers.DataSetStorage;
using DomainDriver.DomainModeling.StorageManagers.EntityFrameworkStorage;
using ObjectRow = DomainDriver.DomainModeling.Example.ExampleModelDataSet.EnumerationDomainObjectsRow;

namespace DomainDriver.DomainModeling.Example.DomainObjects
{
    public partial class EnumerationDomainObject : IEfEntity<int>, IDataSetSerializable<DataSet, DataRow>, IDataSetSerializable<ExampleModelDataSet, DataRow>, IDataSetSerializable<ExampleModelDataSet, ObjectRow>
    {
        #region Entity Framework CTP5 Serialization Code

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int EF_Id
        {
            get { return (int)m_Key; }
            set { m_Key = EnumUtilities.ConvertIntToEnum<EnumerationValue>(value); }
        }

        [Required]
        public int EF_Index
        {
            get { return m_Index; }
            set { m_Index = value; }
        }

        #endregion

        #region DataSet Serialization Methods

        public void Serialize(DataSet datasource, DataRow objectRecord)
        {
            string keyColumnName = ExampleModelDataSet.SchemaHelper.EnumerationDomainObjects.KeyColumn.ColumnName;
            string indexColumnName = ExampleModelDataSet.SchemaHelper.EnumerationDomainObjects.IndexColumn.ColumnName;
            string nameColumnName = ExampleModelDataSet.SchemaHelper.EnumerationDomainObjects.ValueColumn.ColumnName;

            objectRecord[keyColumnName] = EnumUtilities.ConvertEnumToInt<EnumerationValue>(m_Key);
            objectRecord[indexColumnName] = m_Index;
            objectRecord[nameColumnName] = Name;
        }

        public void Deserialize(DataSet datasource, DataRow objectRecord)
        {
            string keyColumnName = ExampleModelDataSet.SchemaHelper.EnumerationDomainObjects.KeyColumn.ColumnName;
            string indexColumnName = ExampleModelDataSet.SchemaHelper.EnumerationDomainObjects.IndexColumn.ColumnName;
            string nameColumnName = ExampleModelDataSet.SchemaHelper.EnumerationDomainObjects.ValueColumn.ColumnName;

            int keyValueAsInt = objectRecord.Field<int>(keyColumnName);
            m_Key = EnumUtilities.ConvertIntToEnum<EnumerationValue>(keyValueAsInt);
            m_Index = objectRecord.Field<int>(indexColumnName);
            // OLD: Name = objectRecord.Field<string>(nameColumnName);
        }

        #endregion

        #region ExampleModelDataSet Serialization Methods

        public string ObjectTableName
        {
            get { return ExampleModelDataSet.SchemaHelper.EnumerationDomainObjects.TableName; }
        }

        public string KeyColumnName
        {
            get { return ExampleModelDataSet.SchemaHelper.EnumerationDomainObjects.KeyColumn.ColumnName; }
        }

        public void Serialize(ExampleModelDataSet datasource, DataRow objectRecord)
        {
            Serialize(datasource, objectRecord as ObjectRow);
        }

        public void Serialize(ExampleModelDataSet datasource, ObjectRow objectRecord)
        {
            objectRecord.Key = EnumUtilities.ConvertEnumToInt<EnumerationValue>(m_Key);
            objectRecord.Index = m_Index;
            objectRecord.Value = Name;
        }

        public void Deserialize(ExampleModelDataSet datasource, DataRow objectRecord)
        {
            Deserialize(datasource, objectRecord as ObjectRow);
        }

        public void Deserialize(ExampleModelDataSet datasource, ObjectRow objectRecord)
        {
            m_Key = EnumUtilities.ConvertIntToEnum<EnumerationValue>(objectRecord.Key);
            m_Index = objectRecord.Index;
            // OLD: Name = objectRecord.Value;
        }

        #endregion
    }
}